package com.lanou.renrenxin.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.adapter.InvitationAdapter;
import com.lanou.renrenxin.base.BaseActivity;
import com.lanou.renrenxin.mybomb.Invitation;
import com.lanou.renrenxin.tools.DividerItemDecoration;
import com.lanou.renrenxin.tools.LogUtils;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by zouhui on 16/3/23.
 */
public class InvitationActivity extends BaseActivity implements InvitationAdapter.InvitationOnItemClickListener, View.OnClickListener {
    private RecyclerView recyclerView;
    private InvitationAdapter adapter;
    private ImageButton backBtn;
    private List<Invitation> data;
    private Handler handler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invitation);
        findViewById(R.id.imgBtn_activity_invitation_back).setOnClickListener(this);
        recyclerView = (RecyclerView) findViewById(R.id.recycle_activity_invitation);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 558){
                    Log.e("∫∫∫∫∫","ssssssssssssss");
                    adapter = new InvitationAdapter(InvitationActivity.this, (List<Invitation>) msg.obj);
                    recyclerView.setAdapter(adapter);
                }
                return false;
            }
        });
        loadData();
    }

    private void loadData() {
        data = new ArrayList<>();
        Log.e("@@@@","ssssssssssssss");
        BmobQuery<Invitation> query = new BmobQuery<>();
        query.addWhereEqualTo("username", EMChatManager.getInstance().getCurrentUser());
        query.findObjects(this, new FindListener<Invitation>() {
            @Override
            public void onSuccess(List<Invitation> list) {
                for (Invitation i : list) {
                    if (!i.getIsAgree()) {
                        handler.sendEmptyMessage(558);
                        LogUtils.e("@@@@", i.getFriendUsername());
                    }else{
                        handler.sendEmptyMessage(580);
                    }
                }
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    @Override
    public void onButtonClick(final String username) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMChatManager.getInstance().acceptInvitation(username);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imgBtn_activity_invitation_back:
                finish();
                break;
        }
    }
}
